﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LanguageDetector
{
    public struct BiGram : IComparable, IComparable<BiGram>, IEquatable<BiGram>
    {
        #region core struct definition
        public BiGram(char a, char b)
        {
            if (a == '\0' || b == '\0')
                throw (new ArgumentOutOfRangeException("a or be can not be '\\0' (empty/null terminator)"));

            A = a;
            B = b;
        }

        public char A;
        public char B;

        #endregion

        #region manual overrides

        public override bool Equals(object obj)
        {
            var b = false;
            if (obj != null && obj is BiGram)
            {
                BiGram other = (BiGram)obj;
                b = Equals(other);
            }
            return b;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public override string ToString()
        {
            return string.Format("BiGram(A={0},B={1})", A, B);
        }

        #endregion

        #region IComparable Members

        public int CompareTo(object obj)
        {
            var c = -5;
            if (obj != null && obj is BiGram)
            {
                BiGram other = (BiGram)obj;
                c = CompareTo(other);
            }
            return c;
        }

        #endregion

        #region IComparable<BiGram> Members

        public int CompareTo(BiGram other)
        {
            return this.A.CompareTo(other.A) * Math.Abs(this.B.CompareTo(other.B));
        }

        #endregion

        #region IEquatable<BiGram> Members

        public bool Equals(BiGram other)
        {
            return this.A == other.A && this.B == other.B;
        }

        #endregion
    }
}
